layui.define(['form','table'], function(exports){
  var $ = layui.$,form = layui.form,table = layui.table;

  var tradeNo = $('input[name="tradeNo"]').val();
    var createDate = $('input[name="createDate"]').val();

  table.render({
    elem: '#LAY-table'
    ,url: layui.setter.webRoot + '/bxscn/shop_order/operate/record/list.data'
    ,where:{tradeNo:tradeNo,createDate:createDate}
    ,method: 'post'
    ,parseData: function (res) {
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.body.total,
            "data": res.body.data
        };
    }
    ,response: {statusCode: 200}
    ,cols: [[
      {field: 'createDate',width:170,title: '操作时间'}
      ,{field: 'content',title: '操作内容'}
      ,{field: 'programTypeName', width:120,title: '处理程序'}
      ,{field: 'operatorType', width:150,title: '操作人类型',templet:function (record){
            return "["+record.operatorTypeName+"]" + record.operator;
      }}
      ,{field: 'state', title: '状态',templet:function (record){
          if(record.state == 1){
              return "成功";
          }else {
              return "失败（"+record.errorMsg+"）";
          }
      }}
    ]]
    ,page: false
    ,height: 'full'
    ,text: '对不起，加载出现异常！'
  });

  //事件
  var active = {
      leaseFinish:function(){
          parent.layer.open({
              type: 2
              ,title: '租借完成操作'
              ,content: layui.setter.webRoot + '/bxscn/shop_order/operate/lease.html?tradeNo='+tradeNo+'&createDate='+createDate
              ,maxmin: false
              ,area: ['800px', '600px']
              ,btn: ['确定', '取消']
              ,yes: function (index, layero) {
                  var iframeWindow = parent.window['layui-layer-iframe' + index]
                      , submitID = 'LAY-form-submit'
                      , submit = layero.find('iframe').contents().find('#' + submitID);
                  iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/bxscn/shop_order/operate/lease.do',
                          data: field,
                          dataType: "json",
                          success: function (res) {
                              if (res.code == 200) {
                                  parent.layer.close(index); //关闭弹层
                                  parent.layer.msg('租借操作成功', {icon: 1},function (){
                                      location.reload();
                                  });
                              } else {
                                  parent.layer.msg(res.msg, {icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      ,revertFinish:function(){
          parent.layer.open({
              type: 2
              ,title: '租借完成操作'
              ,content: layui.setter.webRoot + '/bxscn/shop_order/operate/revert.html?tradeNo='+tradeNo+'&createDate='+createDate
              ,maxmin: false
              ,area: ['800px', '600px']
              ,btn: ['确定', '取消']
              ,yes: function (index, layero) {
                  var iframeWindow = parent.window['layui-layer-iframe' + index]
                      , submitID = 'LAY-form-submit'
                      , submit = layero.find('iframe').contents().find('#' + submitID);
                  iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/bxscn/shop_order/operate/revert.do',
                          data: field,
                          dataType: "json",
                          success: function (res) {
                              if (res.code == 200) {
                                  parent.layer.close(index); //关闭弹层
                                  parent.layer.msg('归还操作成功', {icon: 1},function (){
                                      location.reload();
                                  });
                              } else {
                                  parent.layer.msg(res.msg, {icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      ,extensionLeaseDay:function (){
          parent.layer.open({
              type: 2
              ,title: '延期租借'
              ,content: layui.setter.webRoot + '/bxscn/shop_order/operate/extension_lease_day.html?tradeNo='+tradeNo+'&createDate='+createDate
              ,maxmin: false
              ,area: ['550px', '500px']
              ,btn: ['确定', '取消']
              ,yes: function (index, layero) {
                  var iframeWindow = parent.window['layui-layer-iframe' + index]
                      , submitID = 'LAY-form-submit'
                      , submit = layero.find('iframe').contents().find('#' + submitID);
                  iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/bxscn/shop_order/operate/extension_lease_day.do',
                          data: field,
                          dataType: "json",
                          success: function (res) {
                              if (res.code == 200) {
                                  parent.layer.close(index); //关闭弹层
                                  parent.layer.msg('延期租借操作成功', {icon: 1},function (){
                                      location.reload();
                                  });
                              } else {
                                  parent.layer.msg(res.msg, {icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      ,finish:function (){
          layer.open({
              type: 2
              ,title: '完成便携式电池门店订单'
              ,content: layui.setter.webRoot + '/bxscn/shop_order/operate/finish.html?tradeNo='+tradeNo+'&createDate='+createDate
              ,maxmin: false
              ,area: ['400px', '400px']
              ,btn: ['确定', '取消']
              ,yes: function (index, layero) {
                  var iframeWindow = window['layui-layer-iframe' + index]
                      , submitID = 'LAY-form-submit'
                      , submit = layero.find('iframe').contents().find('#' + submitID);
                  iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/bxscn/shop_order/operate/finish.do',
                          data: field,
                          dataType: "json",
                          success: function (res) {
                              if (res.code == 200) {
                                  layer.close(index); //关闭弹层
                                  layer.msg('完成成功', {icon: 1},function (){
                                      location.reload();
                                  });
                              } else {
                                  layer.msg(res.msg, {icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      ,refund:function(){
          layer.open({
              type: 2
              ,title: '便携式电池门店订单退款'
              ,content: layui.setter.webRoot + '/bxscn/shop_order/operate/refund.html?tradeNo='+tradeNo+'&createDate='+createDate
              ,maxmin: false
              ,area: ['400px', '400px']
              ,btn: ['确定', '取消']
              ,yes: function (index, layero) {
                  var iframeWindow = window['layui-layer-iframe' + index]
                      , submitID = 'LAY-form-submit'
                      , submit = layero.find('iframe').contents().find('#' + submitID);
                  iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/bxscn/shop_order/operate/refund.do',
                          data: field,
                          dataType: "json",
                          success: function (res) {
                              if (res.code == 200) {
                                  layer.close(index); //关闭弹层
                                  layer.msg('退款成功', {icon: 1},function (){
                                      location.reload();
                                  });
                              } else {
                                  layer.msg(res.msg, {icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
     }
      ,cancel:function(){
          layer.open({
              type: 2
              ,title: '撤销便携式电池门店订单'
              ,content: layui.setter.webRoot + '/bxscn/shop_order/operate/cancel.html?tradeNo='+tradeNo+'&createDate='+createDate
              ,maxmin: false
              ,area: ['400px', '300px']
              ,btn: ['确定', '取消']
              ,yes: function (index, layero) {
                  var iframeWindow = window['layui-layer-iframe' + index]
                      , submitID = 'LAY-form-submit'
                      , submit = layero.find('iframe').contents().find('#' + submitID);
                  iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/bxscn/shop_order/operate/cancel.do',
                          data: field,
                          dataType: "json",
                          success: function (res) {
                              if (res.code == 200) {
                                  layer.close(index); //关闭弹层
                                  layer.msg('撤销成功', {icon: 1},function (){
                                      location.reload();
                                  });
                              } else {
                                  layer.msg(res.msg, {icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      ,synMemberOrder:function (){
          layer.confirm('确定要同步会员订单吗？', function(index){
              $.ajax({
                  type: "post",
                  url: layui.setter.webRoot + '/bxscn/shop_order/operate/member/syn.do',
                  data:{tradeNo:tradeNo,createDate:createDate},
                  dataType:"json",
                  success:function(res){
                      if(res.code == 200){
                          layer.msg('同步成功',{icon:1});
                      }else{
                          layer.msg(res.msg,{icon: 5});
                      }
                  }
              });
              layer.close(index);
          });
      }
      ,synExternal:function (){
          layer.confirm('确定要同步第三方订单吗？', function(index){
              $.ajax({
                  type: "post",
                  url: layui.setter.webRoot + '/bxscn/shop_order/operate/external/syn.do',
                  data:{tradeNo:tradeNo,createDate:createDate},
                  dataType:"json",
                  success:function(res){
                      if(res.code == 200){
                          layer.msg('同步成功', {icon: 1},function (){
                              location.reload();
                          });
                      }else{
                          layer.msg(res.msg,{icon: 5});
                      }
                  }
              });
              layer.close(index);
          });
      }
  };
  
  $('.layui-btn.LAY-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });
  
  exports('bxscn_shop_order_info', {})
});